package com.cloudfun.campusshare.entity;

import com.cloudfun.campusshare.common.constant.DeviceModuleStatus;
import com.cloudfun.campusshare.common.constant.DevicePlatform;
import com.cloudfun.campusshare.common.constant.DeviceStatus;
import com.cloudfun.campusshare.common.constant.ProductType;
import com.cloudfun.campusshare.service.UserRelService;
import com.cloudfun.campusshare.util.SpringBeanUtil;
import lombok.*;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

/**
 * @author linyangping
 * @date 2019/11/19
 */
@Data
@Entity
@Builder
@ToString(exclude = {"project"})
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "device")
public class DeviceEntity extends CreatorPersistenceEntity {

    @Id
    @GenericGenerator(name = "id_uuid", strategy = "uuid")
    @GeneratedValue(generator = "id_uuid")
    @Column(name = "id", unique = true, columnDefinition = ("varchar(40) not null comment '主键uuid'"))
    private String id;

    @Column(name = "device_name", columnDefinition = ("varchar(40) default null comment '设备名'"))
    private String deviceName;

    @Column(name = "device_number", columnDefinition = ("varchar(40) default null comment '设备编号'"))
    private String deviceNumber;

    @Column(name = "device_place", columnDefinition = ("varchar(40) default null comment '设备所在场所'"))
    private String devicePlace;

    @Column(name = "address", columnDefinition = ("varchar(255) default null comment '设备具体地址'"))
    private String address;

    @Column(name = "device_type", columnDefinition = ("varchar(40) default null comment '设备型号/设备类型'"))
    private String deviceType;

    @Column(name = "bind_time", columnDefinition = ("varchar(40) default null comment '绑定时间'"))
    private String bindTime;

    @Column(name = "sim_card", unique = true, columnDefinition = ("varchar(40) default null comment 'sim卡号'"))
    private String simCard;

    @Column(name = "qr_code", unique = true, columnDefinition = ("varchar(200) default null comment '设备二维码'"))
    private String qrCode;

    @Column(name = "imei", unique = true, columnDefinition = ("varchar(40) not null comment '设备码、imei'"))
    private String imei;

    @Column(name = "machine_no", unique = true, columnDefinition = ("varchar(40) default null comment '机器码'"))
    private String machineNo;

    @Column(name = "user_id", columnDefinition = ("varchar(40) default null comment '用户id'"))
    private String userId;

    @Column(name = "online", columnDefinition = ("tinyint(1) default null comment '设备是否上线'"))
    private Boolean online;

    @Column(name = "status", columnDefinition = ("varchar(40) not null default 'NOT_RUNNING' comment '设备是否在运行'"))
    @Enumerated(EnumType.STRING)
    private DeviceStatus status;

    @Column(name = "product_type", columnDefinition = ("varchar(40) default null comment '产品类型'"))
    @Enumerated(EnumType.STRING)
    private ProductType productType;

    @Column(name = "last_interaction_time", columnDefinition = ("varchar(40) default null comment '上次交互时间'"))
    private String lastInteractionTime;

    @Column(name = "first_interaction_time", columnDefinition = ("varchar(40) default null comment '首次交互时间'"))
    private String firstInteractionTime;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "project_id", insertable = false, updatable = false)
    private ProjectEntity project;

    @Column(name = "project_id", columnDefinition = ("varchar(40) default null comment '项目id'"))
    private String projectId;

    @Column(name = "school_id", columnDefinition = ("varchar(40) default null comment '学校id'"))
    private String schoolId;

    @Column(name = "platform", columnDefinition = ("varchar(32) default null comment '设备所属平台'"))
    @Enumerated(EnumType.STRING)
    private DevicePlatform platform;

    @Column(columnDefinition = ("varchar(32) default null comment '阿里云产品Key'"))
    private String aliyunProdKey;

    @Column(name = "app_Id", columnDefinition = ("varchar(40) default null comment '应用id'"))
    private String appId;

    @Column(name = "wash_point_id", columnDefinition = ("varchar(40) default null comment '洗衣点Id'"))
    private String washPointId;

    @Column(name = "concrete_position_id", columnDefinition = ("varchar(40) default null comment '洗衣点具体地址Id'"))
    private String concretePositionId;

    @Column(name = "wash_point_name", columnDefinition = ("varchar(64) default null comment '洗衣点名称'"))
    private String washPointName;

    @Column(name = "belong_merchant_name", columnDefinition = ("varchar(64) default null comment '所属商家名称'"))
    private String belongMerchantName;

    @Column(name = "is_using", columnDefinition = ("tinyint(1) not null default 1 comment '是否启用'"))
    private Boolean isUsing;

    @Column(name = "brand_id", columnDefinition = ("varchar(40) default null comment '品牌id'"))
    private String brandId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "brand_id", insertable = false, updatable = false)
    private DeviceBrandEntity deviceBrandEntity;

    @Column(name = "brand_name", columnDefinition = ("varchar(40) default null comment '品牌名称'"))
    private String brandName;

    @Column(name = "service_time_id", columnDefinition = ("varchar(40) default null comment '服务时间id'"))
    private String serviceTimeId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "service_time_id", insertable = false, updatable = false)
    private DeviceServiceTimeEntity deviceServiceTimeEntity;

    @Column(columnDefinition = ("varchar(40) default null comment '设备收费模式集id'"))
    private String modeSetId;

    @Column(name = "iccid", unique = true, columnDefinition = ("varchar(40) default null comment 'iccid'"))
    private String iccid;

    @Column(name = "module_status", columnDefinition = ("varchar(40) default null comment '模块状态'"))
    @Enumerated(EnumType.STRING)
    private DeviceModuleStatus moduleStatus;

    @Column(name = "line_test_time", columnDefinition = ("varchar(40) default null comment '产线测试时间'"))
    private String lineTestTime;

    @Override
    protected void extraOperate() {
        UserRelService userRelService = SpringBeanUtil.getBean(UserRelService.class);
        UserEntity rootUser = userRelService.getUserMerchant(getCreatorId());
        this.userId = rootUser.getId();
        this.belongMerchantName = rootUser.getEnterpriseName();
    }
}
